
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.AccountSecurityInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:账号安全信息请求类
 *
 * @author jerryliu
 * @since 2015年8月20日 下午3:25:18
 */
public class AccountSecurityInfoTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public AccountSecurityInfoResponse request(String username, String kgToken) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        req.setKgToken(kgToken);
        AccountSecurityInfoResponse respObj = new AccountSecurityInfoResponse();
        ResponsePackage resp = new AccountSecurityInfoResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_ACCOUNT_SECURITY_INFO);
        params.put("username", username);
        params.put("kgtoken", kgToken);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_ACCOUNT_SECURITY_INFO);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class AccountSecurityInfoResponsePackage extends
            HttpBaseResponsePackage<AccountSecurityInfoResponse> {

        @Override
        protected void handleResponse(AccountSecurityInfoResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "AccountSecurityInfoTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_ACCOUNT_SECURITY_INFO.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            AccountSecurityInfo info = new AccountSecurityInfo();
                            if (!jsonResult.isNull("safety_score")) {
                                info.setSecurityScore(jsonResult.getInt("safety_score"));
                            }
                            if (!jsonResult.isNull("login_email")) {
                                info.setLoginEmail(jsonResult.getString("login_email"));
                            }
                            if (!jsonResult.isNull("security_email")) {
                                info.setSecurityEmail(jsonResult.getString("security_email"));
                            }
                            if (!jsonResult.isNull("question_id")) {// 未设置安全问题时返回255
                                info.setSecurityQuestionId(jsonResult.getInt("question_id"));
                            }
                            if (!jsonResult.isNull("is_setidcard")) {
                                info.setSetIdentity(jsonResult.getInt("is_setidcard") == 1);
                            }
                            if (!jsonResult.isNull("is_setpaypwd")) {
                                info.setPayPsd(jsonResult.getInt("is_setpaypwd"));
                            }
                            response.setInfo(info);
                            response.setOk(true);
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public static class AccountSecurityInfoResponse extends ResponseMessage {
        private static final long serialVersionUID = -4820735216365527465L;

        private AccountSecurityInfo info;

        public AccountSecurityInfo getInfo() {
            return info;
        }

        public void setInfo(AccountSecurityInfo info) {
            this.info = info;
        }
    }
}
